package com.xindacloud.sms.console.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.system.entity.SmsProjectMember;
import com.xindacloud.sms.console.system.mapper.SmsProjectMemberMapper;
import com.xindacloud.sms.console.system.service.ISmsProjectMemberService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目成员列表 服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */
@Service
public class SmsProjectMemberServiceImpl extends ServiceImpl<SmsProjectMemberMapper, SmsProjectMember> implements ISmsProjectMemberService {

    @Override
    public boolean delSmsProjectMemberByProjectId(String projectId) {
        Wrapper<SmsProjectMember> wrapper = new QueryWrapper<>(SmsProjectMember.builder().projectId(projectId).build());
        return remove(wrapper);
    }

    @Override
    public List<SmsProjectMember> querySmsProjectMemberList(String projectId) {
        return lambdaQuery().eq(SmsProjectMember::getProjectId, projectId)
                .list();
    }

    @Override
    public boolean saveOrUpdateSmsProjectMember(final Collection<SmsProjectMember> collection) {

        if (CollectionUtils.isEmpty(collection)) {
            log.warn("collection is empty.");
            return false;
        }
        final SmsProjectMember next = collection.iterator().next();
        final String projectId = next.getProjectId();
        final boolean hasNull = StringUtils.isEmpty(next.getProjectMemberId()) && Objects.isNull(next.getUserId());
        if (hasNull) {
            return delSmsProjectMemberByProjectId(projectId);
        }
        final List<SmsProjectMember> smsProjectMembers = CollectionUtils.isNotEmpty(querySmsProjectMemberList(projectId)) ?
                querySmsProjectMemberList(projectId) : Collections.EMPTY_LIST;
        if (smsProjectMembers.size() == collection.size() || collection.size() > smsProjectMembers.size()) {
            return saveOrUpdateBatch(collection);
        } else {
            List<String> removeProjectMemberIds = new ArrayList<>();
            List<SmsProjectMember> updateProjectMemberIds = new ArrayList<>();
            for (SmsProjectMember smsProjectMember : collection) {
                for (SmsProjectMember queryRS : smsProjectMembers) {
                    if (Objects.equals(queryRS.getProjectMemberId(), smsProjectMember.getProjectMemberId())) {
                        updateProjectMemberIds.add(smsProjectMember);
                    } else {
                        removeProjectMemberIds.add(queryRS.getProjectMemberId());
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(removeProjectMemberIds)) {
                return this.removeByIds(removeProjectMemberIds);
            }
            if (CollectionUtils.isNotEmpty(updateProjectMemberIds)) {
                return this.updateBatchById(updateProjectMemberIds);
            }

        }
        return false;
    }

    @Override
    public boolean saveSmsProjectMember(Collection<SmsProjectMember> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return false;
        }
        final List<SmsProjectMember> collect = collection.stream()
                .filter(rs -> CollectionUtils.isEmpty(queryProjectMemberFor(rs.getProjectId(), rs.getUserId())))
                .collect(Collectors.toList());
        return saveBatch(collect);
    }

    @Override
    public boolean updateSmsProjectMember(Collection<SmsProjectMember> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return false;
        }
        delSmsProjectMemberByProjectId(collection.iterator().next().getProjectId());
        return super.saveBatch(collection);
    }

    private List<SmsProjectMember> queryProjectMemberFor(String projectId, Integer userId) {
        return super.lambdaQuery()
                .eq(SmsProjectMember::getProjectId, projectId)
                .eq(SmsProjectMember::getUserId, userId)
                .list();

    }
}
